package com.yinshan.printer.service;

import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.yinshan.printer.bean.PrintData;
import com.yinshan.printer.constant.Comm;
import com.yinshan.printer.utils.Logs;

import java.util.ArrayList;

public abstract class BaseIntentService extends IntentService {

    public final String TAG = this.getClass().getSimpleName();

    public BaseIntentService() {
        super("BaseIntentService");
        Logs.d(TAG, "BaseIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Logs.d(TAG, "onHandleIntent");
        if (null == intent) {
            return;
        }
        initPrint();
        ArrayList<PrintData> printDatas = intent.getParcelableArrayListExtra(Comm.DATA_KEY);
        if (null!= printDatas){
            print(printDatas);
        }
    }

    /**
     * 显示toast
     *
     * @param msg msg信息
     */
    public void showToast(final String msg) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseIntentService.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 打印机初始化
     */
    abstract void initPrint();

    /**
     * 打印数据
     */
    abstract void print(ArrayList<PrintData> printDatas);

}
